import type {RouteMeta,RouteRecordRaw} from "vue-router";
import router from "../router/index";
type IRoute = RouteRecordRaw & {id?:string};
// 定义全部路由：admin下的子一级。
const allRoutes:IRoute[] = [
    {
        id:"2-1",
        name:"goodslist",
        path: "goodslist",
        component: () => import("../views/Admin/goods/goodslist.vue")
    },
    {
        id:"2-2",
        name:"goodspaging",
        path: "goodspaging",
        component: () => import("../views/Admin/goods/goodspaging.vue")
    },
    {
        id:"2-3",
        name:"Recommend",
        path: "Recommend",
        component: () => import("../views/Admin/goods/Recommend.vue")
    },
    {
        id:"3-1",
        name:"getbanner",
        path: "getbanner",
        component: () => import("../views/Admin/Banner/getbanner.vue")
    },
    {
        id:"3-2",
        name:"addbanner",
        path: "addbanner",
        component: () => import("../views/Admin/Banner/addbanner.vue")
    },
    {
        id:"4-1",
        name:"listadmin",
        path: "listadmin",
        component: () => import("../views/Admin/Admin/listadmin.vue")
    },
    {
        id:"4-2",
        name:"addadmin",
        path: "addadmin",
        component: () => import("../views/Admin/Admin/addadmin.vue")
    },
   
    {
        id:"5-1",
        name:"getorder",
        path: "getorder",
        component: () => import("../views/Admin/order/getorder.vue")
    },
    {
        id:"6-1",
        name:"address",
        path: "address",
        component: () => import("../views/Admin/address/address.vue")
    },
]      



export default function getRoutes(){
    console.log("getRoutes()");   //跳到首页会执行
    
    // 1、获取当前用户的权限
    let checkedKeys:string[] = [];
    const grants = sessionStorage.getItem("grants");
    if(grants){
        checkedKeys = JSON.parse(grants);
    }
    if(checkedKeys.length===0){
        checkedKeys = ['1','2','2-1',"2-1-r","2-1-s","2-1-m","2-2","2-2-r","2-2-s","2-2-m","2-3","2-3-r","2-3-s","2-3-m",'3','3-1','3-1-r','3-1-u','3-1-d','3-1-e','3-2','4','4-1',"4-1-r","4-1-u","4-1-d",'4-2','5','5-1','6','6-1','7','7-1'];
    }

    // 2、获取当前用户的路由配置

    allRoutes.forEach(item=>{
        if(checkedKeys.includes(item.id as string)){
            // 当前路由权限
            // crud 为 currRoute.meta.crud =1100  //1代表有权限,0代表无权限
            let currRoute = {...item}
            // 先定义一个字符串让它为0000
            let arr = ['0','0','0','0','0']
            if(checkedKeys.includes(item.id+'-c')){
                arr[0]="1"
            }
            if(checkedKeys.includes(item.id+'-r')){
                arr[1]="1"
            }
            if(checkedKeys.includes(item.id+'-u')){
                arr[2]="1"
            }
            if(checkedKeys.includes(item.id+'-d')){
                arr[3]="1"
            }
            // 全部删除
            if(checkedKeys.includes(item.id+'-e')){
                arr[4]="1"
            }
            // 是否推荐
            if(checkedKeys.includes(item.id+'-s')){
                arr[5]="1"
            }
            //是否秒杀
            if(checkedKeys.includes(item.id+'-m')){
                arr[6]="1"
            }
            currRoute.meta={
                crud:arr.join("")
            }
            // 动态添加路由配置： vueRouter对象.addRoute(父级路由的name，路由配置);
            router.addRoute("admin",{...currRoute});
        }
    })
    console.log("router.getRoutes()",router.getRoutes());
}

export const  clearCurrentRoutes = ()=>{
   // 1、获取当前用户的权限
   let checkedKeys:string[] = [];
   const grants =  sessionStorage.getItem("grants");
   if(grants){
       checkedKeys = JSON.parse(grants);
   }
   if(checkedKeys.length===0){
       checkedKeys = ['1','2','2-1',"2-1-r","2-1-s","2-1-m","2-2","2-2-r","2-2-s","2-2-m","2-3","2-3-r","2-3-s","2-3-m",'3','3-1','3-1-r','3-1-u','3-1-d','3-1-e','3-2','4','4-1',"4-1-r","4-1-u","4-1-d",'4-2','5','5-1','6','6-1','7','7-1'];
   }

   // 2、删除当前用户的路由配置

   allRoutes.forEach(item=>{
       if(checkedKeys.includes(item.id as string)){
           // 动态添加路由配置： vueRouter对象.addRoute(父级路由的name，路由配置);
          router.removeRoute(item.name as string);
       }
   })

}